package com.lagou.edu.service.impl;

import com.lagou.edu.dao.AuthCodeDao;
import com.lagou.edu.pojo.LAuthCode;
import com.lagou.edu.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Service("codeService")
public class CodeServiceImpl implements CodeService {

    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private AuthCodeDao authCodeDao;

    @Override
    public int create(String email, String code) {
        LAuthCode lAuthCode = new LAuthCode();
        lAuthCode.setEmail(email);
        Example<LAuthCode> example = Example.of(lAuthCode);

        Optional<LAuthCode> one = authCodeDao.findOne(example);
        if(!one.isEmpty()){
            LAuthCode authCode = one.get();
            if("0".equals(authCode.getState())){
                return 1;
            }
            boolean execute = isRepeatExecute(authCode.getCreatetime());
            if(execute){
                return 2;
            }
            lAuthCode.setId(authCode.getId());
        }

        String now = "";
        String tenLater = "";
        Map timeResult = getTimes();
        if(timeResult != null){
            now = timeResult.get("createtime").toString();
            tenLater = timeResult.get("expiretime").toString();
        }

        // 保存数据
        lAuthCode.setCode(code);
        lAuthCode.setCreatetime(now);
        lAuthCode.setExpiretime(tenLater);
        lAuthCode.setState("1");

        authCodeDao.save(lAuthCode);
        if(lAuthCode.getId()>0){
            return 0;
        }
        return 3;
    }

    // 判断1分钟内是否发送过验证码
    public boolean isRepeatExecute(String time){
        try {
            long nowDate = new Date().getTime();
            long startDateTime = sdf.parse(time).getTime();
            if(nowDate - startDateTime <= 60000){
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    // 获取当前时间和10分钟后的时间
    public Map getTimes(){
        Map result = new HashMap();
        try {
            String now = sdf.format(new Date());
            Date date = sdf.parse(now);
            // 获取10分钟后的时间
            date.setTime(date.getTime()+10*60*1000);
            String tenLater = sdf.format(date);

            result.put("createtime", now);
            result.put("expiretime", tenLater);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }



    @Override
    public int validate(String email, String code) {
        LAuthCode lAuthCode = new LAuthCode();
        lAuthCode.setEmail(email);
        lAuthCode.setCode(code);
        Example<LAuthCode> example = Example.of(lAuthCode);

        Optional<LAuthCode> one = authCodeDao.findOne(example);
        if(!one.isEmpty()){
            LAuthCode authCode = one.get();
            String expiretime = authCode.getExpiretime();

            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            //将字符串形式的时间转化为Date类型的时间
            try {
                Date a = new Date();
                Date b =sdf.parse(expiretime);
                if(a.before(b))
                    return 0;
                else
                    return 2;

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return 1;
    }

}
